import Utils (splitAll)
import Data.List

type Passport =  [(String,String)]

parsePassport :: String -> Passport
parsePassport str = 
    map parseKeyPair keypairs
    where
        keypairs = splitAll "\n " str
    
        parseKeyPair :: String -> (String,String)
        parseKeyPair keypair = ((tokens!!0),(tokens!!1))
            where
                tokens = splitAll ":" keypair

parseInput :: String -> [Passport]
parseInput = map parsePassport . map unlines . splitAll [""] . lines

isPassportValid :: Passport -> Bool
isPassportValid passport = 
    (length passport == 8) || (length passport == 7 && withoutCID passport)
    where
        withoutCID :: Passport -> Bool
        withoutCID = not . elem "cid" . map fst 

main = do
    input <- readFile "./input.txt"
    print . length . filter isPassportValid . parseInput $ input